package com.sttk.fastMark.camera.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.sttk.fastMark.camera.BaseAc;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.bean.PersonalCardBean;
import com.sttk.fastMark.camera.databinding.ActivityPersonalCardBinding;
import com.sttk.fastMark.camera.utils.AppSpUtils;

public class PersonalCardActivity extends BaseAc<ActivityPersonalCardBinding> implements View.OnClickListener {

    public static PersonalCardBean personalCardBean;

    @Override
    protected int onCreate() {
        return R.layout.activity_personal_card;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCustomWaterBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivNameClear.setOnClickListener(this);
        mDataBinding.ivPhoneNumberClear.setOnClickListener(this);
        mDataBinding.llUpload.setOnClickListener(this);
        mDataBinding.tvSave.setOnClickListener(this);

        mDataBinding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivNameClear.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivNameClear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mDataBinding.etPhoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivPhoneNumberClear.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivPhoneNumberClear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivNameClear) {//姓名清空
            mDataBinding.etName.setText("");
        } else if (id == R.id.ivPhoneNumberClear) {//手机号清空
            mDataBinding.etPhoneNumber.setText("");
        } else if (id == R.id.llUpload) {//点击上传
            SelPictureActivity.type = 1;
            startActivity(SelPictureActivity.class);
        } else if (id == R.id.tvSave) {//保存
            String name = mDataBinding.etName.getText().toString();
            String phoneNumber = mDataBinding.etPhoneNumber.getText().toString();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort(getString(R.string.please_input_name));
                return;
            }
            if (TextUtils.isEmpty(phoneNumber)) {
                ToastUtils.showShort(getString(R.string.please_input_phone_number));
                return;
            }
            if (TextUtils.isEmpty(personalCardBean.getImgPath())) {
                ToastUtils.showShort(getString(R.string.please_upload_img));
                return;
            }
            personalCardBean.setName(name);
            personalCardBean.setPhoneNumber(phoneNumber);
            AppSpUtils.savePersonalCard(personalCardBean);
            EditCustomWatermarkActivity.watermarkBean.getCustomBean().setOpenPersonalCard(true);
            finish();
        }
    }

    @Override
    protected void initData() {
        personalCardBean = AppSpUtils.getPersonalCard();
        if (personalCardBean != null) {
            mDataBinding.etName.setText(personalCardBean.getName());
            mDataBinding.etPhoneNumber.setText(personalCardBean.getPhoneNumber());
            Glide.with(mContext).load(personalCardBean.getImgPath()).into(mDataBinding.ivImg);
            mDataBinding.llUpload.setVisibility(View.GONE);
            mDataBinding.ivImg.setVisibility(View.VISIBLE);
        } else {
            personalCardBean = new PersonalCardBean();
            mDataBinding.llUpload.setVisibility(View.VISIBLE);
            mDataBinding.ivImg.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (personalCardBean.getImgPath() != null) {
            Glide.with(mContext).load(personalCardBean.getImgPath()).into(mDataBinding.ivImg);
            mDataBinding.llUpload.setVisibility(View.GONE);
            mDataBinding.ivImg.setVisibility(View.VISIBLE);
        }
    }
}